#!/bin/sh /etc/rc.common
#
# Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
#
# Copyright (C) 2020, Tano Systems LLC. All rights reserved.
# Anton Kikin <a.kikin@tano-systems.com>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
#     * Neither the name of The Linux Foundation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# PM Configuration

START=17

start() {
	soc_id=`cat /sys/devices/soc0/soc_id`
	echo "SOC ID: ${soc_id}" > /dev/kmsg

	case "$soc_id" in
		"290" | "296" | "297" | "298" | "299")
			echo "Power configuration for MDM9607" > /dev/kmsg

			echo "interactive" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
			echo 40000 > /sys/devices/system/cpu/cpufreq/interactive/above_hispeed_delay
			echo 86 > /sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load
			echo 800000 > /sys/devices/system/cpu/cpufreq/interactive/hispeed_freq
			echo "1 800000:86" > /sys/devices/system/cpu/cpufreq/interactive/target_loads
			echo 40000 > /sys/devices/system/cpu/cpufreq/interactive/min_sample_time
			echo 400000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq
			timeout_val=40000000000
			echo app_init $timeout_val > /sys/power/wake_lock

#			echo mem > /sys/power/autosleep
			echo off > /sys/power/autosleep

			# Enable Low Power modes
			echo 0 > /sys/module/lpm_levels/parameters/sleep_disabled

			# Enable FTRACE_ENABLE on MDM9607
			# echo "++++ $0 -> ENABLE-FTRACE" > /dev/kmsg

			# bail out if its perf config
			if [ ! -d /sys/module/msm_rtb ]
			then
				return
			fi

			# bail out if coresight isn't present
			if [ ! -d /sys/bus/coresight ]
			then
				return
			fi

			# bail out if ftrace events aren't present
			if [ ! -d /sys/kernel/debug/tracing/events ]
			then
				return
			fi

			# Coresight
			echo 1 > /sys/bus/coresight/devices/coresight-stm/enable
			echo 1 > /sys/kernel/debug/tracing/tracing_on
			echo 0 > /sys/kernel/debug/tracing/events/enable
			echo 1 > /sys/bus/coresight/devices/coresight-tmc-etf/curr_sink
			echo mem > /sys/bus/coresight/devices/coresight-tmc-etr/out_mode
			echo 1 > /sys/bus/coresight/devices/coresight-tmc-etr/curr_sink

			# Workqueue
			echo 1 > /sys/kernel/debug/tracing/events/workqueue/enable
			echo 1 > /sys/kernel/debug/tracing/events/workqueue/filter
			echo 1 > /sys/kernel/debug/tracing/events/workqueue/workqueue_activate_work/enable
			echo 1 > /sys/kernel/debug/tracing/events/workqueue/workqueue_execute_end/enable
			echo 1 > /sys/kernel/debug/tracing/events/workqueue/workqueue_execute_start/enable
			echo 1 > /sys/kernel/debug/tracing/events/workqueue/workqueue_queue_work/enable

			# SoftIRQs
			echo 1 > /sys/kernel/debug/tracing/events/irq/enable
			echo 1 > /sys/kernel/debug/tracing/events/irq/filter
			echo 1 > /sys/kernel/debug/tracing/events/irq/softirq_entry/enable
			echo 1 > /sys/kernel/debug/tracing/events/irq/softirq_exit/enable
			echo 1 > /sys/kernel/debug/tracing/events/irq/softirq_raise/enable

			# IRQs
			echo 1 > /sys/kernel/debug/tracing/events/irq/enable
			echo 1 > /sys/kernel/debug/tracing/events/irq/irq_handler_entry/enable
			echo 1 > /sys/kernel/debug/tracing/events/irq/irq_handler_exit/enable

			# echo "++++ $0 -> END" > /dev/kmsg
			# FTRACE ENABLE END
			;;
	esac
}

stop() {
	return 0
}

restart() {
	stop
	start
}
